package com.zxkj.commonpatrol.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.LoadingDialog;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.picker.widget.OptionsPickerView;
import com.xuexiang.xui.widget.picker.widget.builder.OptionsPickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnOptionsSelectListener;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.ProjectDetailAdapt;
import com.zxkj.commonpatrol.adapt.ProjectListAdapt;
import com.zxkj.commonpatrol.adapt.SelectProjectAdapt;
import com.zxkj.commonpatrol.databinding.ActivityProjectListBinding;
import com.zxkj.commonpatrol.model.entity.SelectProjectEntrity;
import com.zxkj.commonpatrol.model.response.ProjectDetailResponse;
import com.zxkj.commonpatrol.model.response.RouteSystemResponse;
import com.zxkj.commonpatrol.viewmodel.ProjectListViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ProjectListActivity extends BaseVmDataBindingActivity<ActivityProjectListBinding, ProjectListViewModel> {

    private RecyclerView recyclerView;
    private ProjectListAdapt adapt;
    private int colorAccent;
    private int btn_yellow;
    private int green;
    private int white;
    private int mCurrent=0;
    private boolean isShow=false;
    private OptionsPickerView<Object> mVehicleType;
    private int mVehicleTypeOption;
    private HashMap<String, String> hashMap;
    private String proCode;
    private  String Fifter;
    private String RouteTypeNo;
    private SmartRefreshLayout smartRefreshLayout;
    private MiniLoadingDialog loadingDialog;
    private String ProName;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_project_list;
    }

    @Override
    public int initVariableId() {
        return BR.ProjectListViewModel;
    }


    @Override
    protected void initUI() {
         proCode = getIntent().getStringExtra("ProCode");
        ProName = getIntent().getStringExtra("ProName");
         binding.tvTitle.setText(ProName);
        white= getResources().getColor(R.color.white);
        colorAccent = getResources().getColor(R.color.colorAccent);
        btn_yellow = getResources().getColor(R.color.btn_yellow);
        green = getResources().getColor(R.color.green);
        LinearLayoutManager layoutManager =new LinearLayoutManager(this);
        recyclerView = (RecyclerView) findViewById(R.id.recy_project_project);
        recyclerView.setLayoutManager(layoutManager);
         smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout_recy_project);
    }

    @Override
    protected void initData() {
        if (loadingDialog==null){
            loadingDialog= new MiniLoadingDialog(this, "加載中...");
        }
        loadingDialog.show();
        binding.multipleProjectListView.showLoading();

        viewModel.getTypeData();
        viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        //pending
        binding.tvUpcoming.setOnClickListener(v->{

            if (mCurrent==0){
                //不處理
            }else{
                binding.clDash.setBackgroundColor(btn_yellow);
                binding.tvUpcoming.setEnabled(false);
                binding.tvUpcoming.setTextColor(btn_yellow);
                binding.tvPending.setEnabled(true);
                binding.tvPending.setTextColor(white);
                binding.tvReject.setEnabled(true);
                binding.tvReject.setTextColor(white);
                //刷新數據

                mCurrent=0;
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }

                loadingDialog.show();
                binding.multipleProjectListView.showLoading();
                viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
            }

        });
        //completeed
        binding.tvPending.setOnClickListener(v->{
            if (mCurrent==1){

            }else{
                binding.clDash.setBackgroundColor(green);
                binding.tvUpcoming.setEnabled(true);
                binding.tvUpcoming.setTextColor(white);
                binding.tvPending.setEnabled(false);
                binding.tvPending.setTextColor(green);
                binding.tvReject.setEnabled(true);
                binding.tvReject.setTextColor(white);
                //刷新數據
                //刷新數據
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                mCurrent=1;
                binding.multipleProjectListView.showLoading();
                viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
            }

        });
        //all
        binding.tvReject.setOnClickListener(v->{
            if (mCurrent!=-1){
                binding.clDash.setBackgroundColor(colorAccent);
                binding.tvUpcoming.setEnabled(true);
                binding. tvUpcoming.setTextColor(white);
                binding. tvPending.setEnabled(true);
                binding. tvPending.setTextColor(white);
                binding. tvReject.setEnabled(false);
                binding. tvReject.setTextColor(colorAccent);
                //刷新數據
                if (loadingDialog==null){
                    loadingDialog= new MiniLoadingDialog(this, "加載中...");
                }
                loadingDialog.show();
                mCurrent=-1;
                binding.multipleProjectListView.showLoading();
                viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
            }

        });
        binding.ivSearch.setOnClickListener(v->{
            if (!isShow){
                binding.llHead.setVisibility(View.VISIBLE);
                isShow=true;
            }else{
                binding.llHead.setVisibility(View.GONE);
                isShow=false;
                RouteTypeNo="";
                binding.edSelectProject.setTextEx("請選擇路線");
                Fifter="";
                binding.edSeach.setTextEx("");
            }
        });
        binding.edSelectProject.setOnClickListener(v->{
            if (mVehicleType!=null){
                mVehicleType.show();
            }


        });
        binding.qkBack.setOnClickListener(v->{
            ActivityTool.getInstance().popOneActivity(this);
        });

        binding.ivHeadSearch.setOnClickListener(v->{
            Fifter=binding.edSeach.getTextEx();
            binding.multipleProjectListView.showLoading();
            viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
            }
        });
        binding.multipleProjectListView.setOnRetryClickListener(v -> {
            binding.multipleProjectListView.showLoading();
            viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
        });
        binding.edSeach.setOnFocusChangeListener((v,h)->{
            if (h){
                binding.ivHeadSearch.setImageResource(R.drawable.search_biss);
            }else{
                binding.ivHeadSearch.setImageResource(R.drawable.search);
            }
        });

    }

    @Override
    protected void dataObserver() {
        super.dataObserver();

        viewModel.DetailDatas.observe(this, new Observer<StateData<List<ProjectDetailResponse.Route>>>() {
            @Override
            public void onChanged(StateData<List<ProjectDetailResponse.Route>> listStateData) {
                smartRefreshLayout.finishRefresh();
               if (loadingDialog!=null){
                   loadingDialog.dismiss();
               }
                if (listStateData.getStatus()==StateData.DataStatus.SUCCESS){

                    if (listStateData.getData().size()>0){
                        binding.multipleProjectListView.showContent();
                        if (adapt==null){
                            adapt =new ProjectListAdapt(R.layout.item_projects_list,listStateData.getData(),ProName);
                            recyclerView.setAdapter(adapt);
                            adapt.setmDataCallBack(new ProjectListAdapt.MyProjectDetailCallBack() {
                                @Override
                                public void onDataSuccessCallBack(Object o) {
                                    int i=(Integer)o;

                                    Bundle bundle=new Bundle();
                                    bundle.putString("RouteInspectNo",adapt.getData().get(i).getCode());
                                    bundle.putString("RouteTypeCode",adapt.getData().get(i).getRouteTypeCode());
                                    bundle.putString("Name",adapt.getData().get(i).getName());
                                    startActivity(MProfileActivity.class,bundle);
                                }

                                @Override
                                public void onDataFailCallBack(Object o, Object t1) {

                                }
                            });


                        }else{
                            adapt.setNewData(listStateData.getData());
                            recyclerView.setAdapter(adapt);
                        }
                    }else{
                        binding.multipleProjectListView.showEmpty();
                    }
                }else{
                    binding.multipleProjectListView.showError();
                }
            }
        });



        viewModel.Typedata.observe(this, new Observer<StateData<List<RouteSystemResponse.RouteSystemsBean>>>() {
            @Override
            public void onChanged(StateData<List<RouteSystemResponse.RouteSystemsBean>> listStateData) {
                if (listStateData.getStatus()==StateData.DataStatus.SUCCESS){
                    if (listStateData.getData().size()>0){
                        ArrayList<String> list=new ArrayList();
                        list.add("請選擇路線");
                        hashMap =new HashMap<>();
                        hashMap.put("請選擇路線","");
                        for (int i = 0; i <listStateData.getData().size(); i++) {
                             list.add(listStateData.getData().get(i).getName());
                            hashMap.put(listStateData.getData().get(i).getName(),listStateData.getData().get(i).getCode());
                        }
                        int size4 = list.size();
                        String[] array4 = (String[]) list.toArray(new String[size4]);
                        VehicleType(array4);
                    }

                }
            }
        });
    }

    private void VehicleType(final String[] o) {
        mVehicleType = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public boolean onOptionsSelect(View view, int options1, int options2, int options3) {
                binding.edSelectProject.setText(o[options1]);
                mVehicleTypeOption = options1;
                RouteTypeNo=hashMap.get(o[options1]);
                viewModel.getDatas(1,50,mCurrent,proCode,RouteTypeNo,Fifter);
                return false;
            }


        }).setCancelText("取消")//取消按钮文字
                .setSubmitText("確認")//确认按钮文字
                .setTitleText("請選擇路線")
                .setSubCalSize(16)
                .setSubCalSize(16)

                .setSelectOptions(mVehicleTypeOption)
                .build();
        mVehicleType.setPicker(o);

    }
}
